package com.liuyu.common.framework.weixin4j;

import com.foxinmy.weixin4j.cache.RedisCacheStorager;
import com.foxinmy.weixin4j.qy.WeixinProxy;
import com.liuyu.common.framework.properties.PowerProxyProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @author LiuYu
 * @Project:common-framework
 * @Title:${TITLE}
 * @Description:${todo}
 * @date 2017/7/26 002617:17
 * @Copyright:liuyu
 * @version:V1.0
 */
@Configuration
public class Weixin4jConfig {

    @Autowired
    private RedisProperties redisProperties;


    @Autowired
    private PowerProxyProperties powerProxyProperties;


    @Bean
    public RedisCacheStorager redisCacheStorager() {
        return new RedisCacheStorager(redisProperties.getHost(), redisProperties.getPort(), 5000, redisProperties.getPassword());
    }

    //注意事项： 在设置代理必须在创建weixinProxy 之前，否则代理不会生效。

    /**
     * 设置代理。
     *
     * @return
     */
    @Bean("proxySetting")
    public Weixin4jProxySetting proxySetting() {
        Weixin4jProxySetting weixin4jProxySetting = new Weixin4jProxySetting(powerProxyProperties);
        weixin4jProxySetting.init();
        return weixin4jProxySetting;
    }

    @DependsOn("proxySetting")//通过这个强制依赖，使代理的bean  proxySetting 优先创建。
    @Bean("weixinProxy")
    public WeixinProxy weixinProxy() {
        return new WeixinProxy(redisCacheStorager());
    }

}
